package com.part2.action.common.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class ValueObjectAssembler implements java.io.Serializable {

	/**
	 * A HashMap that stores all value objects
	 */
	private HashMap mValues;
	/**
	 * A HashMap that stores all value object list
	 */
	private HashMap mValueLists;

	public ValueObjectAssembler() {
		mValues = new HashMap();
		mValueLists = new HashMap();
	}

	public void reset() {
		mValues.clear();
		mValueLists.clear();
	}

	/**
	 * Clone another ValueObjectAssembler,including all of its values and value
	 * arrays, to this ValueObjectAssembler
	 * 
	 * @param pValueObjectAssembler
	 */
	public void clone(ValueObjectAssembler pValueObjectAssembler) {
		if (pValueObjectAssembler != null) {
			mValues = pValueObjectAssembler.getValues();
			mValueLists = pValueObjectAssembler.getLists();
		}
	}

	/**
	 * Add another ValueObjectAssembler,including all of its value and value
	 * array, into this ValueObjectAssembler and keep the original values and
	 * value arrays
	 * 
	 * @param pValueObjectAssembler
	 */
	public void add(ValueObjectAssembler pValueObjectAssembler) {
		if (pValueObjectAssembler != null) {
			mValues.putAll(pValueObjectAssembler.getValues());
			mValueLists.putAll(pValueObjectAssembler.getLists());
		}
	}

	/**
	 * Sets a value object by its key
	 */
	public void setValue(String pKey, ValueObject pValue) {
		mValues.put(pKey, pValue);
	}

	/**
	 * Gets a value object by its key
	 * 
	 * @param pKey
	 * @return a value object
	 */
	public ValueObject getValue(String pKey) {
		ValueObject vo = (ValueObject) mValues.get(pKey);
		if (vo == null) {
			vo = new ValueObject();
			setValue(pKey, vo);
		}
		return vo;
	}

	/**
	 * Sets an array of BussinessObject by a key
	 */
	public void setList(String pKey, ValueObject[] pValueList) {
		mValueLists.put(pKey, pValueList);
	}

	/**
	 * Sets a list of BussinessObject by a key
	 */
	public void setList(String pKey, ArrayList pList) {
		if (pList == null) {
			return;
		}

		ValueObject[] mList = new ValueObject[pList.size()];

		for (int i = 0; i < mList.length; i++) {
			mList[i] = (ValueObject) pList.get(i);
		}

		mValueLists.put(pKey, mList);
	}

	/**
	 * Gets value object array by its key
	 * 
	 * @param pKey
	 * @return
	 */
	public ValueObject[] getList(String pKey) {
		return (ValueObject[]) mValueLists.get(pKey);
	}

	/**
	 * Sets all value objects
	 */
	public void setValues(HashMap pValues) {
		mValues = pValues;
	}

	/**
	 * Gets all value objects
	 */
	public HashMap getValues() {
		return mValues;
	}

	/**
	 * Sets all value arrays
	 */
	public void setLists(HashMap pValueLists) {
		mValueLists = pValueLists;
	}

	/**
	 * Gets all value arrays
	 */
	public HashMap getLists() {
		return mValueLists;
	}

	public String toString() {
		StringBuffer ret = new StringBuffer();
		Iterator keys = mValues.keySet().iterator();
		ret.append("\nValues:");
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":" + getValue(key));
		}
		keys = mValueLists.keySet().iterator();
		ret.append("\nValueLists:");
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":");
			ValueObject[] vos = getList(key);
			if (vos != null) {
				for (int i = 0; i < vos.length; i++) {
					ret.append("\n" + vos[i]);
				}
			}
		}
		return ret.toString();
	}
}
